//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.huitu.whht.api.core.common.base;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.*;

public class DataUtils implements Serializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataUtils.class);
    private static final String SORT_KEY = "__index";
    private static final Map<String, Object> EMPTY_MAP = new HashMap(0);

    public DataUtils() {
    }

    public static Map<String, Object> list2Map(List<Map<String, Object>> list, String key) {
        if (null == list) {
            return null;
        } else if (0 == list.size()) {
            return EMPTY_MAP;
        } else if (!((Map) list.get(0)).containsKey(key)) {
            LOGGER.warn(" list2Map : list 中不包含指定key,key值:{}", key);
            return null;
        } else {
            Object result;
            if (((Map) list.get(0)).containsKey("__index")) {
                result = new LinkedHashMap(list.size());
            } else {
                result = new HashMap(list.size());
            }

            Iterator var3 = list.iterator();

            while (var3.hasNext()) {
                Map<String, Object> item = (Map) var3.next();
                String value = item.get(key).toString();
                if (!((Map) result).containsKey(value)) {
                    ((Map) result).put(value, item);
                }
            }

            return (Map) result;
        }
    }

    public static List<Map<String, Object>> list2Tree(List<Map<String, Object>> data, String idFieldNm, String pidFieldNm) {
        return list2Tree(data, idFieldNm, pidFieldNm, "children");
    }

    public static List<Map<String, Object>> list2Tree(List<Map<String, Object>> data, String idFieldNm, String pidFieldNm, String childrenFieldNm) {
        List<Map<String, Object>> treeList = new ArrayList();
        Iterator var6 = data.iterator();

        label47:
        while (var6.hasNext()) {
            Map<String, Object> _data1 = (Map) var6.next();
            if (_data1.get(pidFieldNm) == null || StringUtils.isEmpty(_data1.get(pidFieldNm).toString()) || _data1.get(pidFieldNm).toString().equals(_data1.get(idFieldNm).toString())) {
                treeList.add(_data1);
            }

            Iterator var8 = data.iterator();

            while (true) {
                Map _data2;
                do {
                    do {
                        if (!var8.hasNext()) {
                            continue label47;
                        }

                        _data2 = (Map) var8.next();
                    } while (!_data1.get(idFieldNm).equals(_data2.get(pidFieldNm)));
                } while (_data2.get(idFieldNm).equals(_data1.get(idFieldNm)));

                if (_data1.get(childrenFieldNm) == null || !(_data1.get(childrenFieldNm) instanceof ArrayList)) {
                    List<Map<String, Object>> nodeChildren = new ArrayList();
                    _data1.put(childrenFieldNm, nodeChildren);
                }

                ((ArrayList) _data1.get(childrenFieldNm)).add(_data2);
            }
        }

        return treeList;
    }

    public static <T> List<List<T>> list2Lists(List<T> list, Integer size) {
        List<List<T>> lists = new ArrayList();
        Integer listsSize = list.size() % size == 0 ? list.size() / size : list.size() / size + 1;

        for (int i = 0; i < listsSize; ++i) {
            List<T> subList = new ArrayList();
            int j = i * size;

            for (int jMax = Math.min((i + 1) * size, list.size()); j < jMax; ++j) {
                subList.add(list.get(j));
            }

            lists.add(subList);
        }

        return lists;
    }

    public static Map<String, List<Map<String, Object>>> listMap2MapList(List<Map<String, Object>> list, String key) {
        Map<String, List<Map<String, Object>>> result = new LinkedHashMap();
        String _key;
        Map data;
        if (list != null && list.size() > 0) {
            for (Iterator var4 = list.iterator(); var4.hasNext(); ((List) result.get(_key)).add(data)) {
                data = (Map) var4.next();
                _key = (String) data.get(key);
                if (!result.containsKey(_key)) {
                    result.put(_key, new ArrayList());
                }
            }
        }

        return result;
    }

    public static Double getMaxNum(List<Double> list) {
        return list.size() == 1 ? (Double) list.get(0) : (Double) list.stream().max((a, b) -> {
            return a.compareTo(b);
        }).get();
    }
}
